package com.qf.controller;

import com.qf.pojo.DtsCategory;
import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/admin/dashboard")
public class DtsAdminController {

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsUserService userService;

    @Autowired
    DtsGoodsProductService goodsProductService;

    @Autowired
    DtsOrderService orderService;

    @Autowired
    DtsCategoryService categoryService;

    @RequestMapping("/chart")
    public Object chart(){
        //categorySell
        List<CategorySellAmts> categorySellList = categoryService.findCategorySell();

        String[] categoryNames = new String[categorySellList.size()];
        for (int i = 0; i < categorySellList.size(); i++) {
            CategorySellAmts categorySellAmts = categorySellList.get(i);
            categoryNames[i] = categorySellAmts.getName();
        }

        CategorySellVo categorySellVo = new CategorySellVo();
        categorySellVo.setCategoryNames(categoryNames);
        categorySellVo.setCategorySellData(categorySellList);


        //orderAmts
        List<DayStatis> orderAmts = orderService.findOrderAmts();

        List<String> dayDataList = orderAmts.stream().map(orderStati -> {
            return orderStati.getDayStr();
        }).collect(Collectors.toList());
        String[] dayDate = dayDataList.toArray(new String[]{});

        List<BigDecimal> orderAmtDataList = orderAmts.stream().map(orderStati -> {
            return orderStati.getAmts();
        }).collect(Collectors.toList());
        BigDecimal[] orderAmtData = orderAmtDataList.toArray(new BigDecimal[]{});

        List<Integer> orderCntDataList = orderAmts.stream().map(dayStatis -> {
            return dayStatis.getCnts();
        }).collect(Collectors.toList());
        Integer[] orderCntData = orderCntDataList.toArray(new Integer[]{});

        OrderAmtsVo orderAmtsVo = new OrderAmtsVo();
        orderAmtsVo.setDayData(dayDate);
        orderAmtsVo.setOrderAmtData(orderAmtData);
        orderAmtsVo.setOrderCntData(orderCntData);

        //userOrderCnt
        List<DayStatis> userDayStatis = userService.findUserOrderCnt();
        List<String> dayDataLists = userDayStatis.stream().map(dayStatis -> {
            return dayStatis.getDayStr();
        }).collect(Collectors.toList());
        String[] dayData = dayDataLists.toArray(new String[]{});
        List<Integer> userCntList = userDayStatis.stream().map(dayStatis -> {
            return dayStatis.getCnts();
        }).collect(Collectors.toList());
        Integer[] userCnt = userCntList.toArray(new Integer[]{});

        UserOrderCntVo userOrderCnt = new UserOrderCntVo();
        userOrderCnt.setDayData(dayData);
        userOrderCnt.setUserCnt(userCnt);
        userOrderCnt.setOrderCnt(orderCntData);

        Map<String,Object> data = new HashMap<>();
        data.put("categorySell",categorySellVo);
        data.put("orderAmts",orderAmtsVo);
        data.put("userOrderCnt",userOrderCnt);

        return ResponseUtil.ok(data);
    }

    @RequestMapping("")
    public Object init(){
        Map<String,Object> data = new HashMap<>();

        long goodsTotal = goodsService.getTotal();
        long userTotal = userService.getTotal();
        long productTotal = goodsProductService.getTotal();
        long orderTotal = orderService.getTotal();

        data.put("goodsTotal", goodsTotal);
        data.put("userTotal", userTotal);
        data.put("productTotal",productTotal );
        data.put("orderTotal", orderTotal);

        return ResponseUtil.ok(data);
    }
}
